package com.meitutest.autoscript.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Calendar;

public class TestReport {
    private static final int CLOSE = 10;
    private static final int OPEN = 0;

    private static final int INFO = 2;
    private static final int ERROR = 3;
    private static final int FAIL = 4;
    private static final int SUCCESS = 5;

    private static final int CRASH = 1000;

    private static int logfile_switch = OPEN;
    private static int logcat_switch = OPEN;

    private static String log_path = "/sdcard/autotest/autoscript";
    ;// Environment.getExternalStorageDirectory().getPath();
    private static String log_default_tag = "autoscript";
    private static String log_file_head = "autoscript";

    public static final void setLogFileSwitch(int nSwitch) {
        if (nSwitch > CLOSE || nSwitch < OPEN)
            return;
        logfile_switch = nSwitch;
    }

    public static final void setLogCatSwitch(int nSwitch) {
        if (nSwitch > CLOSE || nSwitch < OPEN)
            return;
        logcat_switch = nSwitch;
    }

    public static boolean isShowI() {
        return logfile_switch <= INFO || logcat_switch <= INFO;
    }

    public static boolean isShowE() {
        return logfile_switch <= ERROR || logcat_switch <= ERROR;
    }

    public static boolean isShowF() {
        return logfile_switch <= FAIL || logcat_switch <= FAIL;
    }

    public static boolean isShowS() {
        return logfile_switch <= SUCCESS || logcat_switch <= SUCCESS;
    }

    public static int i(String msg) {
        return i(log_default_tag, msg);
    }

    public static int i(String tag, String msg) {
        return println(INFO, tag, msg);
    }

    public static int e(String msg) {
        return e(log_default_tag, msg);
    }

    public static int e(String tag, String msg) {
        return println(ERROR, tag, msg);
    }

    public static int f(String msg) {
        return e(log_default_tag, msg);
    }

    public static int f(String tag, String msg) {
        return println(FAIL, tag, msg);
    }

    public static int s(String msg) {
        return s(log_default_tag, msg);
    }

    public static int s(String tag, String msg) {
        return println(SUCCESS, tag, msg);
    }

    public static String getLogPath() {
        return log_path;
    }

    public static boolean checkLogPath(String newPath) {
        if (newPath == null) {
            return false;
        }
        boolean ret = false;
        try {
            File logPath = new File(newPath);
            if (!logPath.exists()) {
                logPath.mkdirs();
            }
            if (logPath.isDirectory()) {
                ret = true;
            } else {
                ret = false;
            }
        } catch (Throwable tr) {
            ret = false;
        }
        return ret;
    }

    public static final boolean setLogPath(String newPath) {
        boolean ret = checkLogPath(newPath);
        if (ret) {
            log_path = newPath;
        }
        return ret;
    }

    public static String getStackTraceString(Throwable tr) {
        if (tr == null) {
            return "";
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        tr.printStackTrace(pw);
        return sw.toString();
    }

    private static int printToFile(int priority, String tag, String type,
                                   byte[] buffer, int offset, int count) {
        int ret = 0;
        String logpath = getLogPath();
        if (priority == CRASH) {
            type = "crash";
        }
        if (priority < logfile_switch || !checkLogPath(logpath)) {
            if (priority == CRASH) {
                logpath = "/sdcard/";
                type = "crash";
                if (!checkLogPath(logpath)) {
                    return ret;
                }
            } else {
                return ret;
            }
        }
        Thread.currentThread();
        // Calendar cal =
        // Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int millisecond = cal.get(Calendar.MILLISECOND);

        String timeString = String.format("%d-%02d-%02d %02d:%02d:%02d.%d",
                year, month, day, hour, minute, second, millisecond);
        String headString = String.format("\r\n%s\t(%d)\ttag:%s\tdata:",
                timeString, priority, tag);
        byte[] headBuffer = headString.getBytes();

        String fileName;
        switch (priority) {
            case INFO:
                fileName = "%s/" + log_file_head + "_Info%d%02d%02d.%s";
                break;
            case ERROR:
                fileName = "%s/" + log_file_head + "_Error%d%02d%02d.%s";
                break;
            case FAIL:
                fileName = "%s/" + log_file_head + "_Fail%d%02d%02d.%s";
                break;
            case SUCCESS:
                fileName = "%s/" + log_file_head + "_Success%d%02d%02d.%s";
                break;
            default:
                fileName = "%s/%d%02d%02d.%s";
        }
        fileName = String.format(fileName, logpath, year, month, day, type);
        FileOutputStream fo = null;
        try {
            File file = new File(fileName);
            fo = new FileOutputStream(file, true);
            fo.write(headBuffer);
            fo.write(buffer, offset, count);
            ret = headBuffer.length + count;
        } catch (Throwable tr) {
            ret = 0;
        } finally {
            if (fo != null) {
                try {
                    fo.close();
                } catch (Throwable tr) {
                }
            }
        }
        return ret;
    }

    public static int printMsgToFile(int priority, String tag, String msg) {
        if (msg == null) {
            msg = "[null]";
        }
        byte[] buffer = msg.getBytes();
        if (tag != null && tag.startsWith("autotest")) {
            printToFile(priority, tag, "autotest", buffer, 0, buffer.length);
        }
        return printToFile(priority, tag, "txt", buffer, 0, buffer.length);
    }

    private static int println(int priority, String tag, String msg) {
        int ret = 0;
        if (priority >= logcat_switch) {
            ret += android.util.Log.println(priority, tag, msg);
        }
        if (priority >= logfile_switch) {
            ret += printMsgToFile(priority, tag, msg);
        }
        return ret;
    }

}
